﻿using System.Windows;
using System.Windows.Media;

namespace SegmentDisplay.Common.Element
{
    /// <summary>
    /// Control with single segment eleemnt
    /// </summary>
    public class SegmentElement : SegmentBase
    {
            
        #region Input values

        /// <summary>
        /// Input value 
        /// </summary>
        public bool Value
        {
            get { return (bool)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        
        /// <summary>
        /// The dependency property that gets or sets the Value.
        /// </summary>
        public static DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(bool), typeof(SegmentElement), new PropertyMetadata(false, new PropertyChangedCallback(ValueChanged)));
        
        /// <summary>
        /// Function on change Value property 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SegmentElement sh = (SegmentElement)d;
            var newValue = (bool)e.NewValue;
            sh.UpdateSegments(sh.IsSwitchOn, newValue);
        }

        /// <summary>
        /// Changing on change a value 
        /// </summary>
        /// <param name="isSwitchOn"></param>
        /// <param name="newValue"></param>
        protected void UpdateSegments(bool isSwitchOn, bool newValue)
        {
            if (isSwitchOn)
                SegmentDP = newValue ? LightOn : LightOff;
            else
                SegmentDP = LightOff;
        }
        
        /// <summary>
        /// Default update with current values
        /// </summary>
        protected void UpdateSegments()
        {
            UpdateSegments(IsSwitchOn, Value);
        }
        #endregion

        #region Management of serment

        /// <summary>
        /// Override a behavior on change a IsSwitchOn property
        /// </summary>
        /// <param name="newValue"></param>
        public override void OnIsSwitchOnChanged(bool newValue)
        {
            base.OnIsSwitchOnChanged(newValue);
            UpdateSegments(newValue, Value);
        }

        #endregion

        #region Segments
        /// <summary>
        /// Gets or sets the source of the SegmentDP property.
        /// </summary>
        public Brush SegmentDP
        {
            get { return (Brush)GetValue(SegmentDPProperty); }
            set { SetValue(SegmentDPProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the SegmentDP property.
        /// </summary>
        public static DependencyProperty SegmentDPProperty = 
            DependencyProperty.Register("SegmentDP", typeof(Brush), typeof(SegmentElement), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        #endregion

        #region Constructors

        /// <summary>
        /// Static constructor
        /// </summary>
        static SegmentElement()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SegmentElement), new FrameworkPropertyMetadata(typeof(SegmentElement)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public SegmentElement()
        {
            this.DefaultStyleKey = typeof(SegmentElement);
        }

        #endregion
    }
}
